﻿myApp.controller("ReceiveLotteryCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', '$location', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope, $location) { 
    var product_id = $routeParams.id;
    if (!window.localStorage.getItem('isgetproduct') || window.localStorage.getItem('isgetproduct') != product_id) {
        window.location.href = "#/usercenter";
        return;
    }
   
    var sale = 0;
    $scope.Address = {};
    $scope.Product = {};
    //购买数量
    $scope.num = 1;
    //总价(包括运费)
    $scope.total = 0;
    //单价
    $scope.sale = 0;
    //运费
    $scope.GetAddessList = function () {
        $http({ method: "GET", url: $restUrls["getCustomerInfo"], params: {} }).success(function (result) {
            if (result.success == 1) {
                $scope.Address = result.data.default_addressinfo;
                $scope.CouponList = result.data.coupons;
            }
        })
    }
    $scope.GetProductDetail = function (product_id) {
        $http({ method: "GET", url: $restUrls["GetProductDetail"], params: { product_id: product_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.Product = result.data.product;
            }
        })
    }
    $scope.GetProductDetail(product_id);
    $scope.GetAddessList();
    $scope.Add_Address = function () {
        window.location.href = "#/add_address";
    }

    
    //生成订单
    $scope.paying = false;
    $scope.PayOrder = function () {
        if (!$scope.Address.address_id) {
            $.scojs_message('请选择或添加收货地址', $.scojs_message.TYPE_ERROR);
            return;
        }
        if ($scope.paying) {
            return;
        }
        $scope.paying = true;
        $post($restUrls["receiveLottery"], { product_id: product_id || 0, quantity: $scope.num }).success(function (result) {
            if (result.success == 1) {
                window.localStorage.removeItem('isgetproduct');
                window.location.href = "#/orderlist";
            }
            else {
                $scope.paying = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.paying = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        })
    };
}]);